From 134edb20c1c9ecd6cc72213fc6879dbd60d014f1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jes=C3=BAs=20Espino?= Date: Mon, 7 Mar 2016 09:19:40 +0100 Subject: [PATCH] Adding --limit usage suggestion on search results (#2402) --- Cargo.lock | 1 + src/bin/cargo.rs | 1 + src/cargo/ops/registry.rs | 22 +++++++++++++++++++++- src/crates-io/Cargo.toml | 1 + src/crates-io/lib.rs | 18 +++++++++++++----- 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3c3b25d2b..705299f15 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,6 +73,7 @@ version = "0.1.0" dependencies = [ "curl 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", + "url 0.2.38 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/src/bin/cargo.rs b/src/bin/cargo.rs index 0e0071323..f01efb2bc 100644 --- a/src/bin/cargo.rs +++ b/src/bin/cargo.rs @@ -1,4 +1,5 @@ extern crate cargo; +extern crate url; extern crate env_logger; extern crate git2_curl; extern crate rustc_serialize; diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index da2d80022..a02b80a6b 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -10,6 +10,8 @@ use git2; use registry::{Registry, NewCrate, NewCrateDependency}; use term::color::BLACK; +use url::percent_encoding::{percent_encode, QUERY_ENCODE_SET}; + use core::source::Source; use core::{Package, SourceId}; use core::dependency::Kind; @@ -353,7 +355,7 @@ pub fn search(query: &str, } let (mut registry, _) = try!(registry(config, None, index)); - let crates = try!(registry.search(query, limit).map_err(|e| { + let (crates, total_crates) = try!(registry.search(query, limit).map_err(|e| { human(format!("failed to retrieve search results from the registry: {}", e)) })); @@ -381,5 +383,23 @@ pub fn search(query: &str, try!(config.shell().say(line, BLACK)); } + let search_max_limit = 100; + if total_crates > limit as u32 && limit < search_max_limit { + try!(config.shell().say( + format!("... and {} crates more (use --limit N to see more)", + total_crates - limit as u32), + BLACK) + ); + } else if total_crates > limit as u32 && limit >= search_max_limit { + try!(config.shell().say( + format!( + "... and {} crates more (go to http://crates.io/search?q={} to see more)", + total_crates - limit as u32, + percent_encode(query.as_bytes(), QUERY_ENCODE_SET) + ), + BLACK) + ); + } + Ok(()) } diff --git a/src/crates-io/Cargo.toml b/src/crates-io/Cargo.toml index 70068ec5b..298d4707a 100644 --- a/src/crates-io/Cargo.toml +++ b/src/crates-io/Cargo.toml @@ -14,4 +14,5 @@ path = "lib.rs" [dependencies] curl = "0.2" +url = "0.2" rustc-serialize = "0.3" diff --git a/src/crates-io/lib.rs b/src/crates-io/lib.rs index 0586c76f2..4e49dacd9 100644 --- a/src/crates-io/lib.rs +++ b/src/crates-io/lib.rs @@ -1,4 +1,5 @@ extern crate curl; +extern crate url; extern crate rustc_serialize; use std::collections::HashMap; @@ -14,6 +15,8 @@ use curl::http::handle::Method::{Put, Get, Delete}; use curl::http::handle::{Method, Request}; use rustc_serialize::json; +use url::percent_encoding::{percent_encode, QUERY_ENCODE_SET}; + pub struct Registry { host: String, token: Option, @@ -88,7 +91,8 @@ pub struct User { #[derive(RustcDecodable)] struct ApiError { detail: String } #[derive(RustcEncodable)] struct OwnersReq<'a> { users: &'a [&'a str] } #[derive(RustcDecodable)] struct Users { users: Vec } -#[derive(RustcDecodable)] struct Crates { crates: Vec } +#[derive(RustcDecodable)] struct TotalCrates { total: u32 } +#[derive(RustcDecodable)] struct Crates { crates: Vec, meta: TotalCrates } impl Registry { pub fn new(host: String, token: Option) -> Registry { @@ -170,11 +174,15 @@ impl Registry { Ok(()) } - pub fn search(&mut self, query: &str, limit: u8) -> Result> { - let body = try!(self.req(format!("/crates?q={}&per_page={}", query, limit), None, Get, - Auth::Unauthorized)); + pub fn search(&mut self, query: &str, limit: u8) -> Result<(Vec, u32)> { + let formated_query = percent_encode(query.as_bytes(), QUERY_ENCODE_SET); + let body = try!(self.req( + format!("/crates?q={}&per_page={}", formated_query, limit), + None, Get, Auth::Unauthorized + )); - Ok(json::decode::(&body).unwrap().crates) + let crates = json::decode::(&body).unwrap(); + Ok((crates.crates, crates.meta.total)) } pub fn yank(&mut self, krate: &str, version: &str) -> Result<()> { -- 2.30.2